<template>
  <div class="login">
    <h1>后台管理系统</h1>
    <div class="login-box">
      <el-form :model="form" :rules="rules" ref="submitForm" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-button"
            type="primary"
            @click.stop="submitForm('submitForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
      <div class="login-p">
        <p class="text">温馨提示</p>
        <p class="text">未登录过的新用户，自动注册</p>
        <p class="text">注册过的用户可凭账号密码登录</p>
      </div>
    </div>
  </div>
</template>
<script>
// @ is an alias to /src
import { login } from "@/api/api";
import { mapActions, mapState } from "vuex";
export default {
  mounted() {
    if (!this.adminInfo.id) {
      this.getAdminData();
    }
  },
  watch: {
    adminInfo: function (newValue) {
      if (newValue.id) {
        this.$message({
          type: "success",
          message: "检测到您之前登录过，将自动登录",
        });
        this.$router.push("dashboard");
      }
    },
  },
  computed: {
    ...mapState(["adminInfo"]),
  },
  methods: {
    ...mapActions(["getAdminData"]),
    submitForm: function (formName) {
      this.$refs[formName].validate(async (valid) => {
        // console.log(valid);
        // console.log(this.form);
        if (valid) {
          let res = await login({
            user_name: this.form.username,
            password: this.form.password,
          });
          console.log(res);
          if (res.status === 1) {
            this.$message({
              type: "success",
              message: res.success,
            });
            this.$router.push("dashboard");
          } else {
            this.$message({
              type: "error",
              message: "登录出现问题！",
            });
          }
        } else {
          this.$notify({
            title: "账号密码不能为空",
            message: "请输入正确的账号密码",
            type: "error",
          });
          return false;
        }
      });
    },
  },
  data: function () {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: {
          required: true,
          message: "请输入账号",
          trigger: "blur",
        },
        password: {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
      },
    };
  },
};
</script>
<style lang="less">
@import url("../style/mixin.less");
.login {
  .tlc();
  h1 {
    color: white;
    // color:white;
    text-align: center;
    font-weight: 200;
    padding: 20px;
  }
  .login-box {
    border-radius: 10px;
    padding: 10px;
    background-color: white;
    .wh(300px,300px);
    .login-form {
      width: 90%;
      margin: 0 auto;
      .login-button {
        width: 100%;
      }
    }
    .login-p {
      .text {
        text-align: center;
        color: red;
        font-size: 14px;
      }
    }
  }
}
</style>
